package com.settlement.system.controller;

import com.settlement.system.common.enums.CostAllocationEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblRrVoucherType;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblRrVoucherTypeService;
import com.settlement.system.common.util.common.DateUtil;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.*;

/**
 * 收退款-凭证类型 前端控制器
 * @author Ximi
 * @since 2020-07-06
 */

@Api(tags = "收退款-凭证类型",description = "收退款-凭证类型")
@RestController
@RequestMapping("/rr/voucher_type")
public class TblRrVoucherTypeController extends ExcelController<TblRrVoucherType> {

    @Resource
    private ITblRrVoucherTypeService tblRrVoucherTypeService;

    @Override
    public IBaseService init() {
        return tblRrVoucherTypeService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblRrVoucherType.class;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"shareOrNot","beforeOrAfterTicket");
        super.setParamsModel(model);
    }

    @Override
    public String verifyImportObject(TblRrVoucherType tblRrVoucherType, AnalysisContext analysisContext) {
        String message = super.verifyImportObject(tblRrVoucherType,analysisContext);
        if(StringUtils.isNotEmpty(message)){
            return message;
        }
        try {
            List<TblRrVoucherType> tblRrVoucherTypes = tblRrVoucherTypeService.selectByParams(tblRrVoucherType);
            if(!CollectionUtils.isEmpty(tblRrVoucherTypes)){
                return "数据已存在";
            }else{
                TblRrVoucherType bySort = tblRrVoucherTypeService.getBySort(tblRrVoucherType.getSort());
                if(bySort!=null){
                    return "排序已存在";
                }

                if(CostAllocationEnum.BeforeOrAfterTicketEnum.NOT_INVOLVE.getNo().equals(tblRrVoucherType.getBeforeOrAfterTicket())
                        && !CostAllocationEnum.ShareOrNotEnum.NO.getNo().equals(tblRrVoucherType.getShareOrNot())
                ){
                    return "当【票前票后】=[不涉及]时，对应【是否分摊】只能为“否”";
                }

                TblRrVoucherType byVoucherTypeAndShareOrNot = tblRrVoucherTypeService.
                        getByVoucherTypeAndNotShareOrNot(tblRrVoucherType.getVoucherType(), tblRrVoucherType.getShareOrNot());
                if(byVoucherTypeAndShareOrNot!=null){
                    return "同个凭证类型【是否分摊】只能是同时为[是]或[否]";
                }

                TblRrVoucherType byVoucherTypeAndBeforeOrAfterTicket = tblRrVoucherTypeService.
                        getByVoucherTypeAndNotBeforeOrAfterTicket(tblRrVoucherType.getVoucherType(), tblRrVoucherType.getBeforeOrAfterTicket());
                if(byVoucherTypeAndBeforeOrAfterTicket!=null){
                    return "同个凭证类型【票前票后】只能是同时为[票前]或[票后]或[不涉及]";
                }




            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return "校验时查询出错";
        }

        return null;
    }

    @Override
    protected String validateAllDate(List<TblRrVoucherType> datas) {
        Map<String,String>voucherTypeShareOrNotMap = new HashMap<>();
        for (TblRrVoucherType data : datas) {
            String voucherType = data.getVoucherType();
            String shareOrNotInMap = voucherTypeShareOrNotMap.get(voucherType);
            if(StringUtils.isEmpty(shareOrNotInMap)){
                voucherTypeShareOrNotMap.put(voucherType,data.getShareOrNot());
            }else{
                String shareOrNot = data.getShareOrNot();
                if(!shareOrNot.equals(shareOrNotInMap)){
                    return "同个凭证类型【是否分摊】只能是同时为[是]或[否]";
                }
            }

        }
        Map<String,String> beforeOrAfterTicketMap = new HashMap<>();
        for (TblRrVoucherType data : datas) {
            String voucherType = data.getVoucherType();
            String beforeOrAfterTicketInMap = beforeOrAfterTicketMap.get(voucherType);
            if(StringUtils.isEmpty(beforeOrAfterTicketInMap)){
                beforeOrAfterTicketMap.put(voucherType,data.getBeforeOrAfterTicket());
            }else{
                String beforeOrAfterTicket = data.getBeforeOrAfterTicket();
                if(!beforeOrAfterTicket.equals(beforeOrAfterTicketInMap)){
                    return "同个凭证类型【票前票后】只能是同时为[票前]或[票后]或[不涉及]";
                }
            }

        }
        return super.validateAllDate(datas);
    }

    @Override
    protected void saveData(List<TblRrVoucherType> datas) throws ServiceException {

        List<String> list = new ArrayList<>();
        for(TblRrVoucherType data : datas){
            String toString = data.toString();
            if(list.contains(toString)){
                throw new ServiceException("有两行数据重复");
            }else{
                list.add(toString);
            }
        }

        list = new ArrayList<>();
        for(TblRrVoucherType data : datas){
            String sort = data.getSort();
            if(list.contains(sort)){
                throw new ServiceException("有两行排序重复");
            }else{
                list.add(sort);
            }
        }




        super.saveData(datas);
    }
}

